import {
  queryIndicators,
  queryIdentify,
  queryMetas,
} from '../services/index';

export default {
    namespace: 'gis',
  
    state: {
      IndicatorsResource: {},
      IdentifyResource: [],
      MetasResource: [],
      AgeResource: {},
    },
  
    effects: {
      *queryIndicators({ payload }, { call, put }) {
        const data = yield call(queryIndicators, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              IndicatorsResource: other.features[0].attributes,
            },
          })
        } else {
          throw data
        }
      },
      *queryAnalysis({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(queryIndicators, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`AnalysisResource${type}`]: other.features,
            },
          })
        } else {
          throw data
        }
      },
      /**
       * 管龄统计
       */
      *queryAge({ payload: params }, { call, put }){
        const { key, ...payload } = params;
        const data = yield call(queryIndicators, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`AgeResource${key}`]: other.features[0].attributes,
            },
          })
        } else {
          throw data
        }
      },
      /**
       * 管网设备统计
       */
      *queryIdentify({ payload }, { call, put }) {
        const data = yield call(queryIdentify, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              IdentifyResource: other.results,
            },
          })
        } else {
          throw data
        }
      },
      *queryMetas({ payload }, { call, put }) {
        const data = yield call(queryMetas, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              MetasResource: other.metainfo,
            },
          })
        } else {
          throw data
        }
      },
    },
  
    reducers: {
      setState(state, action) {
        return {
            ...state,
            ...action.payload
        };
      }
    },
  }
  